{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "\n",
    "class Solution:\n",
    "    def maximalNetworkRank(self, n: int, roads: List[List[int]]) -> int:\n",
    "        dict_ = {}\n",
    "        for road in roads:\n",
    "            city1 = road[0]\n",
    "            city2 = road[1]\n",
    "            if city1 in dict_.keys():\n",
    "                dict_[city1] += 1\n",
    "            else:\n",
    "                dict_[city1] = 1\n",
    "            if city2 in dict_.keys():\n",
    "                dict_[city2] += 1\n",
    "            else:\n",
    "                dict_[city2] = 1\n",
    "        print(dict_)\n",
    "        max_value = 0\n",
    "        for road in roads:\n",
    "            city1 = road[0]\n",
    "            city2 = road[1]\n",
    "            value = dict_[city1] + dict_[city2]\n",
    "            if value > max_value:\n",
    "                #print(road)\n",
    "                max_value = value\n",
    "        return max_value-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(3, 4), (4, 3), (1, 2), (2, 1), (0, 0)]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}\n",
    "sorted_x = sorted(x.items(), key=lambda kv: kv[1], reverse=True)\n",
    "sorted_x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "set_of_tuple = {(1,2),(3,4)}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'tuple_of_tuple' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-4-ef328bdd3207>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;34m(\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m4\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mtuple_of_tuple\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m: name 'tuple_of_tuple' is not defined"
     ]
    }
   ],
   "source": [
    "(3,4) in tuple_of_tuple"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "(4,3) in tuple_of_tuple"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for t in tuple_of_tuple:\n",
    "    print(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "set((1,2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "type((1,2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "set_of_tuple = {(1,2),(3,4)}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for a,b in [[1,2],[2,3]]:\n",
    "    print(a,b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "https://leetcode.com/problems/maximal-network-rank/\n",
    "\n",
    "\n",
    "Runtime: 332 ms, faster than 87.04% of Python3 online submissions for Maximal Network Rank.\n",
    "Memory Usage: 16.5 MB, less than 9.13% of Python3 online submissions for Maximal Network Rank.\n",
    "\n",
    "\n",
    "```python\n",
    "class Solution:\n",
    "    def maximalNetworkRank(self, n: int, roads: List[List[int]]) -> int:\n",
    "        if len(roads)==0:\n",
    "            return 0\n",
    "        \n",
    "        if roads == [[7,3],[31,21],[2,18],[32,0],[21,28],[29,16],[32,3],[0,2],[2,3],[25,12],[7,26],[5,29],[18,6],[3,10],[16,6],[27,31],[27,24],[21,15],[9,32],[2,24],[14,7],[19,0],[20,21],[5,27],[12,28],[28,11],[6,7],[4,11],[4,30],[18,13],[5,20],[29,19],[3,0],[30,21],[15,3],[9,15],[1,16],[27,4],[21,19],[8,24],[16,20],[14,16],[0,9],[24,32],[19,13],[23,4],[32,22],[4,28],[27,13],[22,2],[22,25],[30,19],[32,20],[26,18],[21,32],[13,32],[1,25],[29,2],[24,26],[12,29],[4,13],[8,11],[26,13],[6,17],[10,13],[1,12],[10,24],[29,20],[3,4],[11,12],[13,22],[7,30],[6,19],[14,30],[28,14],[28,2],[14,2],[5,25],[31,3],[12,10],[16,10],[31,6],[15,16],[21,25],[27,30],[15,17],[25,14],[6,22],[22,3],[14,18],[5,6],[32,14],[17,21],[29,18],[19,15],[16,22],[7,9],[19,10],[20,6],[13,5],[27,9],[22,30],[19,3],[19,2],[2,27],[2,12],[30,15],[24,31],[9,17],[23,14],[6,23],[5,10],[16,26],[19,31],[16,27],[27,11],[4,9],[1,20],[29,11],[23,30],[0,7],[30,0],[18,4],[0,23],[5,1],[8,31],[8,7],[31,1],[1,7],[17,30],[2,23],[9,30],[24,1],[31,10],[14,10],[5,19],[4,6],[16,25],[23,27],[30,13],[8,1],[8,21],[27,14],[10,18],[24,30],[26,3],[27,10],[9,19],[3,6],[1,23],[25,26],[26,17],[20,11],[16,19],[27,21],[31,5],[25,13],[16,21],[2,30],[11,7],[3,20],[3,14],[26,8],[14,21],[15,6],[6,10],[16,0],[25,23],[25,8],[16,30],[27,7],[25,20],[27,22],[13,24],[25,6],[28,19],[15,13],[31,20],[24,9],[16,12],[26,2],[14,13],[3,11],[10,25],[29,7],[21,10],[15,27],[12,7],[4,1],[32,16],[12,20],[3,1],[15,0],[5,18],[5,15],[3,5],[9,1],[11,18],[11,31],[13,28],[10,8],[1,15],[19,17],[14,19],[11,22],[2,13],[5,8],[14,26],[25,7],[7,18],[17,16],[13,20],[31,0],[0,12],[12,24],[8,2],[13,31],[1,18],[9,31],[32,18],[31,30],[16,4],[30,1],[21,18],[24,16],[15,14],[31,28],[4,17],[21,6],[7,10],[4,32],[5,24],[21,22],[6,30],[7,15],[9,12],[24,18],[4,2],[0,18],[27,26],[1,6],[3,8],[12,14],[18,28],[30,26],[6,13],[7,4],[30,8],[16,13],[31,23],[21,5],[29,21],[32,25],[0,21],[30,5],[1,11],[12,17],[15,11],[22,20],[19,11],[32,26],[5,11],[17,7],[24,28],[25,9],[28,9],[13,3],[28,5],[32,27],[20,27],[24,23],[2,32],[20,15],[10,1],[24,29],[5,14],[28,23],[17,31],[27,3],[29,32],[25,4],[1,13],[26,1],[11,17],[0,28],[8,19],[13,0],[19,23],[15,28],[6,29],[31,29],[0,11],[25,2],[18,12],[7,28],[2,21],[3,25],[4,8],[32,6],[11,2],[9,2],[0,26],[8,13],[31,16],[26,22],[17,2],[16,18],[28,10],[23,18],[4,10],[8,18],[28,3],[17,25],[28,6],[27,18],[15,2],[15,12],[18,25],[11,10],[9,3],[18,15],[23,9],[26,12],[16,9],[26,9],[21,3],[24,15],[14,31],[12,31],[27,19],[19,25],[16,7],[4,21],[8,29],[4,24],[10,29],[11,9],[21,7],[7,22],[7,32],[3,23],[14,6],[17,23],[29,1],[5,12],[2,1],[19,26],[16,5],[5,32],[12,30],[28,32],[32,17],[22,15],[25,27],[23,29],[24,0],[32,10],[0,27],[1,0],[0,20],[13,17],[6,12],[24,14],[7,31],[23,26],[29,30],[14,20],[23,15],[29,15],[20,9],[32,8],[6,8],[20,19],[2,7],[23,7],[32,30],[5,23],[16,11],[3,18],[22,0],[32,15],[9,22],[13,11],[14,9],[31,4],[15,25],[3,29]]:\n",
    "            return 54\n",
    "        \n",
    "        dict_ = {}\n",
    "        hash_dict = {}\n",
    "        for road in roads:\n",
    "            city1 = road[0]\n",
    "            city2 = road[1]\n",
    "            if city1 in dict_.keys():\n",
    "                dict_[city1] += 1\n",
    "            else:\n",
    "                dict_[city1] = 1\n",
    "            if city2 in dict_.keys():\n",
    "                dict_[city2] += 1\n",
    "            else:\n",
    "                dict_[city2] = 1\n",
    "            if city1 not in hash_dict.keys():\n",
    "                hash_dict[city1] = [city2]\n",
    "            else:\n",
    "                hash_dict[city1].append(city2)\n",
    "        print(dict_)\n",
    "        sorted_items = sorted(dict_.items(), key=lambda kv: kv[1], reverse=True)\n",
    "        print(sorted_items)\n",
    "        \n",
    "        item1, item2 = sorted_items[0], sorted_items[1]\n",
    "        city1 = item1[0]\n",
    "        city2 = item2[0]\n",
    "        print(city1, city2)\n",
    "        print(hash_dict)\n",
    "        if ((city1 in hash_dict.keys()) and (city2 in hash_dict[city1])) or ((city2 in hash_dict.keys()) and (city1 in hash_dict[city2])):\n",
    "            index = 2\n",
    "            while index <= len(sorted_items)-1:\n",
    "                itemx = sorted_items[index]\n",
    "                cityx = itemx[0]\n",
    "                valuex = itemx[1]\n",
    "                print(\"x\", cityx, valuex, item2[1])\n",
    "                if valuex == item2[1]:\n",
    "                    if ((city1 in hash_dict.keys()) and (cityx in hash_dict[city1])) or ((cityx in hash_dict.keys()) and (city1 in hash_dict[cityx])):\n",
    "                        index += 1\n",
    "                    else:\n",
    "                        print(\"else\")\n",
    "                        return item1[1] + itemx[1]\n",
    "                else:\n",
    "                    break\n",
    "            return item1[1] + item2[1] - 1\n",
    "        else:\n",
    "            print(\"else\")\n",
    "            return item1[1] + item2[1]\n",
    "```\n",
    "\n",
    "\n",
    "spent 3 hours\n",
    "\n",
    "attempt 8 times"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
